package com.jbm.jbmsupplier.activity.address;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import com.jbm.jbmsupplier.net.NetAddRess;
import com.jbm.jbmsupplier.view.DialogLoading;
import com.jbm.onehome.iface.IGeoinfo;

import java.util.ArrayList;
import java.util.List;

import static com.jbm.jbmsupplier.activity.address.AddressEditActivity.ADDRESSMESAGE;


/**
 * Created by JBMhaier on 2017/8/8.
 * 地址页面的model
 */

public class AddressModel extends Activity{
    //地区 ,小区 ,楼门  ,户号
    private List<IGeoinfo> mDiQu,mXiaoQu,mLouMen,mHuhao;

    private  Context context;

    //地区
    private   String city="";
    //小区
    private   String  xiaoQu="";
    //楼门
    private   String  louMen="";
    //户号
    private   String  huHao="";

    private DialogLoading dialogLoading;
    private NetAddRess netAddRess;

    public void setClickType(int clickType) {
        this.clickType = clickType;
    }

    //点击了哪个Spinner
    private int clickType=0;

    private Spinner[] spArr;
    private ArrayAdapter<IGeoinfo> mAdapter1,mAdapter2,mAdapter3,mAdapter4;
    private final String error="没有我的地址";
    private final String defaultValue="请选择地址";
    private  StringBuffer buff;
    //户号的类型
    private String huHaoType;
    /**
     * 默认初始化header
     *
     *
     */
    public AddressModel( Context context, Handler mHandler, Spinner[] spArr) {
        this.context=context;
        this.spArr=spArr;
        dialogLoading = new DialogLoading(context, "加载中...", true);
         netAddRess = new NetAddRess(mHandler);
         buff=new StringBuffer();
        initDate();
    }

    private void initDate() {
        mDiQu= new ArrayList<IGeoinfo>();
        mXiaoQu= new ArrayList<IGeoinfo>();
        mLouMen= new ArrayList<IGeoinfo>();
        mHuhao= new ArrayList<IGeoinfo>();
//        //获取地区
        getNetData(0,0,"120100000000","","");
    }

    //调用层级
    public void getNetData(int gi_id, int prev_id, String prev_code, String type ,String gi_type ){
        dialogLoading.show();
        netAddRess.GetGeoinfo(gi_id, prev_id, prev_code, type,gi_type);
    }

    /**
     * 提交地址
     * @param mobile
     * @param contacts
     */
    public void commitAddress(String mobile,String contacts,String gi_type){
        dialogLoading.show();
        netAddRess.AddAddressList(buff.toString(), mobile,contacts, ADDRESSMESAGE,gi_type);
    }



        //设置地区数据
    public void setDate(IGeoinfo[] arr,Spinner mCityCtrl){
        //将户号置无
        huHaoType="";
        IGeoinfo info=new IGeoinfo();
        info.gi_name=defaultValue;
        switch (clickType){
            case 0:
                mDiQu.clear();;
                mDiQu.add(info);
                break;
            case 1:
                mXiaoQu.clear();
                mXiaoQu.add(info);
                break;
            case 2:
                mLouMen.clear();;
                mLouMen.add(info);
                break;
            case 3:
                mHuhao.clear();;
                mHuhao.add(info);
                break;
        }

        for (int i = 0; i < arr.length; i++) {
    switch (clickType){
        case 0:
            mDiQu.add(arr[i]);
            break;
        case 1:
            mXiaoQu.add(arr[i]);
            break;
        case 2:
            mLouMen.add(arr[i]);
            break;
        case 3:
            mHuhao.add(arr[i]);
            break;
    }}
        IGeoinfo info1=new IGeoinfo();
        info1.gi_name=error;
        switch (clickType){
            case 0:
//                mDiQu.add(info1);
                break;
            case 1:
                mXiaoQu.add(info1);
                break;
            case 2:
                mLouMen.add(info1);
                break;
            case 3:
                mHuhao.add(info1);
                break;
        }

        switch (clickType){
            case 0:
                mAdapter1 = new ArrayAdapter<IGeoinfo>(context, android.R.layout.simple_spinner_item, mDiQu);
                mAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spArr[clickType].setAdapter(mAdapter1);
                break;
            case 1:
                mAdapter2 = new ArrayAdapter<IGeoinfo>(context, android.R.layout.simple_spinner_item, mXiaoQu);
                mAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spArr[clickType].setAdapter(mAdapter2);
                break;
            case 2:
                mAdapter3 = new ArrayAdapter<IGeoinfo>(context, android.R.layout.simple_spinner_item, mLouMen);
                mAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spArr[clickType].setAdapter(mAdapter3);
                break;
            case 3:
                mAdapter4 = new ArrayAdapter<IGeoinfo>(context, android.R.layout.simple_spinner_item, mHuhao);
                mAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spArr[clickType].setAdapter(mAdapter4);
                break;
        }
        setSpinnerListener(clickType);

    }
    private void setSpinnerListener(int x){
        switch (x){
            case 0:
                spArr[0].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                        city = mAdapter1.getItem(position).gi_name;
                        xiaoQu="";
                        louMen="";
                        huHao="";
                        if(listener!=null&&!error.equals(city)&&!defaultValue.equals(city)){
                            clickType=1;
                            listener.Click(clickType,mAdapter1.getItem(position).gi_id);
//                            listener.Put(false);
                        }else{
                            if(city.equals(error)){
                                listener.Put(true);
                            }
                        }
                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                break;
            case 1:
                spArr[1].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        xiaoQu = mAdapter2.getItem(position).gi_name;
                        louMen="";
                        huHao="";
                        if(listener!=null&&!xiaoQu.equals(error)&&!xiaoQu.equals(defaultValue)){
                            clickType=2;
                            listener.Click(clickType,mAdapter2.getItem(position).gi_id);
//                            listener.Put(false);
                        }else{
                            clickType=1;
                            if(defaultValue.equals(xiaoQu)){
                                listener.Put(false);
                            }
                            if(xiaoQu.equals(error)){

                                listener.Put(true);
                            }
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                break;
            case 2:
                spArr[2].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        louMen = mAdapter3.getItem(position).gi_name;
                        huHao="";
                        if(listener!=null&&!louMen.equals(error)&&!louMen.equals(defaultValue)){
                            clickType=3;
                            listener.Click(clickType,mAdapter3.getItem(position).gi_id);
//                            listener.Put(false);
                        }else{
                            clickType=2;
                            if(defaultValue.equals(louMen)){
                                listener.Put(false);
                            }
                            if(louMen.equals(error)){
                                listener.Put(true);
                            }
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                break;
            case 3:
                spArr[3].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        huHao = mAdapter4.getItem(position).gi_name;
                        if(listener!=null&&!huHao.equals(error)&&!huHao.equals(defaultValue)){
                            huHaoType=mAdapter4.getItem(position).gi_type;
//                            listener.Put(false);
                        }else{
                            clickType=3;
                            if(defaultValue.equals(huHao)){
                                listener.Put(false);
                            }
                            if(huHao.equals(error)){
                                listener.Put(true);
                            }
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                break;
        }
    }



    public void dismissLoader(){
        if(dialogLoading!=null){
            dialogLoading.dismiss();
        }
    }

    /**
     * 最终获取的地址结果
     * @param et
     * @return
     */
    public String getAddress(EditText et){
        buff.delete(0,buff.length());
        if(city.equals(defaultValue)||city.equals(error)){
            city="";
        }
        if(xiaoQu.equals(defaultValue)||xiaoQu.equals(error)){
            xiaoQu="";
        }
        if(louMen.equals(defaultValue)||louMen.equals(error)){
            louMen="";
        }
        if(huHao.equals(defaultValue)||huHao.equals(error)){
            huHao="";
        }
        return buff.append(city).append(xiaoQu).append(louMen).append(huHao).append(et.getText().toString()).toString();
    }



    public void setBackListener(BackListener listener){
        this.listener=listener;
    }
    private BackListener listener;

    public int getClickType() {
        return clickType;
    }

    public void setHuHaoType(String huHaoType) {
        this.huHaoType = huHaoType;
    }

    public String getHuHaoType() {
        return huHaoType;
    }

    public interface BackListener{
        void Click(int type,int gi_id);
        //使用户输入
        void Put(boolean isVisible);
    }

}
